﻿package de.gameduell.framework.facebook.app.popups {
	import de.gameduell.framework.facebook.app.pyramide.Brick;

	import de.gameduell.framework.debug.Debug;

	import flash.display.Sprite;
	import flash.events.Event;

	public class ShareLevelUpPopup extends PyramidBasePopup {
		
		private var brick:Brick;
		private var rankImage:Sprite;

		public function ShareLevelUpPopup(aBrick:Brick = null) {
			super("shareLevelUpPopup", "shareLevelUpPopup", 0, true);
			brick = aBrick;			
			registerButton("skipButton", hide);
			registerButton("okButton", shareLevelUp);
			
			localeData.setValue("userLevel", String(brick.id));
			Debug.trace("ShareLevelUpPopup::ShareLevelUpPopup: " + "userRank_" + brick.id + " :" + locale.loadString("userRank_" + String(brick.id)));
			
			localeData.setValue("userRank", locale.loadString("userRank_" + String(brick.id)));
			
			rankImage = sprite.getChildByName("rankImage") as Sprite;
//			var imageLoader:Loader = new Loader();
//			imageLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			
			
//			var userLevel:String = int(brick.id) > 10 ? "default" : brick.id; // TODO - we have currently only 10 images!!!
//			var imageUrl:String = locale.loadString("shareLevelUpPopup.imageUrl", true) + "rank_" + userLevel +  "_large.jpg"; // TODO FIXME - remove the imagePath (../../ from FE)
//			var context:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
//			context.checkPolicyFile   = true;
//			rankImage.addChild(imageLoader);
//			try{
//				imageLoader.load(new URLRequest(imageUrl), context);
//			}catch(e:Error){
//				
//			}
		}

//		private function ioErrorHandler(event:IOErrorEvent):void {
//			// FIXME - do something 
//		}
		
		override public function show(event:Event = null):void {
			tracker.track("share_level_up" + brick.id);
			super.show();
		}
		
		private function shareLevelUp(event:Event):void {
			var defaultURL:String = locale.loadString("rankPublishDialog.defaultImage", true); // FIXME - workaround because of no images
			localeData.setValue("rankImageURL", defaultURL); // FIXME - workaround because of no images
//			if(int(brick.id) > 10){
//				var defaultURL:String = locale.loadString("rankPublishDialog.defaultImage", true);
//				localeData.setValue("rankImageURL", defaultURL);
//			}else{
//				localeData.setValue("rankImageURL", localeData.getValue("imageUrl") + "ranks/rank_" + brick.id + "_small.jpg");
//			}
			controller.postRankToWall(localeData.getValue("userRank"));
			hide();
		}
	}
}
